//
//  OilLoginVC.m
//  Base_App
//
//  Created by Yutian Duan on 2019/5/30.
//  Copyright © 2019年 Wanwin. All rights reserved.
//

#import "OilLoginVC.h"
#import "YFBaseTextField.h"
#import "ApplyOilFirstVC.h"
#import "BaseNavigationVC.h"

#define TextFieldFont 13.5f

@interface OilLoginVC () {
  ///! 记录当前视图的状态栏颜色是否是白色
  BOOL isWhiteStates;

}

///! 油咭号码
@property (nonatomic, strong) YFBaseTextField *oilNumberField;
///! 车牌号码
@property (nonatomic, strong) YFBaseTextField *carNumberField;
///! 电话号码
@property (nonatomic, strong) YFBaseTextField *phoneField;
///! 验证码
@property (nonatomic, strong) YFBaseTextField *codeField;
///!
@property (nonatomic, strong) UIButton *codeButton;


@end

@implementation OilLoginVC

- (UIButton *)codeButton {
  if (!_codeButton) {
    _codeButton = [[UIButton alloc] initWithFrame:CGRectMake(_codeField.yt_right + 10, _codeField.yt_top + 4, _codeField.yt_width - 10, _codeField.yt_height-8)];
    _codeButton.titleLabel.font = [UIFont systemFontOfSize:TextFieldFont];
    [_codeButton setTitle:YTLocalizedString(@"获取验证码") forState:UIControlStateNormal];
    [_codeButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    _codeButton.layer.masksToBounds = YES;
    _codeButton.layer.cornerRadius = 8;
    _codeButton.backgroundColor = HEXCOLOR(0xbb2b25);
    [_codeButton addTarget:self action:@selector(sendCode) forControlEvents:UIControlEventTouchUpInside];
  }
  return _codeButton;
}


- (void)viewDidLoad {
  [super viewDidLoad];
  // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated {
  [super viewWillAppear:animated];
  [self.navigationController setNavigationBarHidden:YES animated:animated];
  if (isWhiteStates) {
    [self setStatusBarStyleWhite];
  } else {
    [self setStatusBarStyleBack];
  }
}

- (void)createFields {
  [super createFields];
  
  self.view.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.3];
  
  if ([UIApplication sharedApplication].statusBarStyle == UIStatusBarStyleLightContent) {
    isWhiteStates = YES;
  } else {
    isWhiteStates = NO;
  }
  
}

- (void)createViews {
  [super createViews];

  ///!
  CGFloat imageWith = ScreenWidth - 40;
  
  UIView *contenView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, imageWith , imageWith * 1.2 + 40)];
  contenView.backgroundColor = HEXCOLOR(0x292929);
  contenView.layer.cornerRadius = 8.0f;
  contenView.center = CGPointMake(ScreenWidth/2, (ScreenHeight - TabBarHeight)/2);
  [self.view addSubview:contenView];

  CGFloat cancelH = 28;
  CGFloat logoH = 13;
  CGFloat fontOffset = 0;
  CGFloat leftOffset = 0;
  CGFloat labelW = 80;
  if (IS_IPHONE5) {
    cancelH = 24;
    logoH = 11;
    fontOffset = 2;
    leftOffset = 20;
    labelW = 70;
  }
  
  ///! 取消
  UIButton *cancelButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 20, cancelH, cancelH)];
  [cancelButton setBackgroundImage:[UIImage imageNamed:@"Logoin_cancel"] forState:UIControlStateNormal];
  [cancelButton addTarget:self action:@selector(cancelClick) forControlEvents:UIControlEventTouchUpInside];
  cancelButton.yt_right = contenView.yt_width - 20;
  [contenView addSubview:cancelButton];

  
  //! logo
  UIImageView *logoView = [[UIImageView alloc] initWithFrame:CGRectMake(0, cancelButton.yt_bottom, logoH * 14, logoH)];
  logoView.image = [UIImage imageNamed:@"Login_logo"];
  logoView.contentMode = UIViewContentModeScaleAspectFit;
  [contenView addSubview:logoView];
  logoView.yt_centerX = contenView.yt_width/2;
  
  
  UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(40, logoView.yt_bottom + 20, contenView.yt_width - 80, 30)];
  titleLabel.text = YTLocalizedString(@"添加油咭");
  titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:18.0f - fontOffset];
  titleLabel.textAlignment = NSTextAlignmentCenter;
  titleLabel.textColor = [UIColor whiteColor];
  [contenView addSubview:titleLabel];


  UILabel *oilNumberLabel = [[UILabel alloc] initWithFrame:CGRectMake(40 - leftOffset, titleLabel.yt_bottom + 20, labelW, 35)];
  oilNumberLabel.text = [NSString stringWithFormat:@"%@：",YTLocalizedString(@"油咭号码")];
  oilNumberLabel.textColor = [UIColor whiteColor];
  oilNumberLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:TextFieldFont - fontOffset];
  oilNumberLabel.textAlignment = NSTextAlignmentLeft;
  [contenView addSubview:oilNumberLabel];
  
  
  _oilNumberField = [[YFBaseTextField alloc] initWithFrame:CGRectMake(oilNumberLabel.yt_right, oilNumberLabel.yt_top, contenView.yt_width - oilNumberLabel.yt_right - labelW/2 + leftOffset, oilNumberLabel.yt_height)];
  [_oilNumberField setTextFont:[UIFont systemFontOfSize:TextFieldFont - fontOffset]];
  [_oilNumberField set_placeTextColor:DEF_GRAY];
  _oilNumberField.placeholder = YTLocalizedString(@"输入已登记油咭号码");
  _oilNumberField.layer.masksToBounds = YES;
  _oilNumberField.layer.cornerRadius = 8;
  _oilNumberField.backgroundColor = [UIColor whiteColor];
  _oilNumberField.leftViewMode = UITextFieldViewModeAlways;
  _oilNumberField.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, _oilNumberField.yt_height)];
  [contenView addSubview:_oilNumberField];

  UILabel *carLabel = [[UILabel alloc] initWithFrame:CGRectMake(oilNumberLabel.yt_left, oilNumberLabel.yt_bottom + 20, oilNumberLabel.yt_width, oilNumberLabel.yt_height)];
  carLabel.text = [NSString stringWithFormat:@"%@：",YTLocalizedString(@"车牌号码")];
  carLabel.textColor = [UIColor whiteColor];
  carLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:TextFieldFont - fontOffset];
  carLabel.textAlignment = NSTextAlignmentLeft;
  [contenView addSubview:carLabel];

  _carNumberField = [[YFBaseTextField alloc] initWithFrame:CGRectMake(_oilNumberField.yt_left, carLabel.yt_top, _oilNumberField.yt_width, _oilNumberField.yt_height)];
  [_carNumberField setTextFont:[UIFont systemFontOfSize:TextFieldFont - fontOffset]];
  [_carNumberField set_placeTextColor:DEF_GRAY];
  _carNumberField.placeholder = YTLocalizedString(@"输入登录密码");
  
  _carNumberField.layer.masksToBounds = YES;
  _carNumberField.layer.cornerRadius = 8;
  _carNumberField.backgroundColor = [UIColor whiteColor];
  _carNumberField.leftViewMode = UITextFieldViewModeAlways;
  _carNumberField.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, _phoneField.yt_height)];
  [contenView addSubview:_carNumberField];
  
  UILabel *phoneLabel = [[UILabel alloc] initWithFrame:CGRectMake(carLabel.yt_left, carLabel.yt_bottom + 20, carLabel.yt_width, carLabel.yt_height)];
  phoneLabel.text = [NSString stringWithFormat:@"%@：",YTLocalizedString(@"电话号码")];
  phoneLabel.textColor = [UIColor whiteColor];
  phoneLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:TextFieldFont - fontOffset];
  phoneLabel.textAlignment = NSTextAlignmentLeft;
  [contenView addSubview:phoneLabel];

  _phoneField = [[YFBaseTextField alloc] initWithFrame:CGRectMake(_carNumberField.yt_left, phoneLabel.yt_top, _carNumberField.yt_width, _carNumberField.yt_height)];
  [_phoneField setTextFont:[UIFont systemFontOfSize:TextFieldFont - fontOffset]];
  [_phoneField set_placeTextColor:DEF_GRAY];
  _phoneField.placeholder = YTLocalizedString(@"输入手机号码");
  
  _phoneField.layer.masksToBounds = YES;
  _phoneField.layer.cornerRadius = 8;
  _phoneField.backgroundColor = [UIColor whiteColor];
  _phoneField.leftViewMode = UITextFieldViewModeAlways;
  _phoneField.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, _phoneField.yt_height)];
  [contenView addSubview:_phoneField];
  
  _codeField = [[YFBaseTextField alloc] initWithFrame:CGRectMake(_phoneField.yt_left, _phoneField.yt_bottom + 20, _phoneField.yt_width / 2, _phoneField.yt_height)];
  [_codeField setTextFont:[UIFont systemFontOfSize:TextFieldFont - fontOffset]];
  [_codeField set_placeTextColor:DEF_GRAY];
  _codeField.placeholder = YTLocalizedString(@"输入短讯验证码");
  
  _codeField.layer.masksToBounds = YES;
  _codeField.layer.cornerRadius = 8;
  _codeField.backgroundColor = [UIColor whiteColor];
  _codeField.leftViewMode = UITextFieldViewModeAlways;
  _codeField.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, _codeField.yt_height)];
  [contenView addSubview:_codeField];
  
  
  [contenView addSubview:self.codeButton];

  
  CGFloat buttonH = 30;
  if (IS_IPHONE5) {
    buttonH = 28;
  }
  ///! 申请
  UIButton *registButton = [[UIButton alloc] initWithFrame:CGRectMake(0, _codeField.yt_bottom + 20, 5 * buttonH, buttonH)];
//  [registButton setImage:[UIImage imageNamed:@"Oil_CardRegist"] forState:UIControlStateNormal];
  
  registButton.backgroundColor = DEF_GRAY;
  registButton.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:15];
  [registButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  [registButton setTitle:YTLocalizedString(@"油咭申请") forState:UIControlStateNormal];
  registButton.layer.masksToBounds = YES;
  registButton.layer.cornerRadius = 8;
  
  registButton.yt_right = contenView.yt_width/2 - 10;
  [registButton addTarget:self action:@selector(cardRegistClick) forControlEvents:UIControlEventTouchUpInside];
  [contenView addSubview:registButton];
  
  ///!
  UIButton *submitButton = [[UIButton alloc] initWithFrame:CGRectMake(registButton.yt_right + 20, registButton.yt_top, registButton.yt_width, registButton.yt_height)];
//  [submitButton setImage:[UIImage imageNamed:@"Oil_CardBind"] forState:UIControlStateNormal];
 
  submitButton.backgroundColor = DEF_RED;
  submitButton.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:15];
  [submitButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  [submitButton setTitle:YTLocalizedString(@"绑定") forState:UIControlStateNormal];
  submitButton.layer.masksToBounds = YES;
  submitButton.layer.cornerRadius = 8;

  
  [submitButton addTarget:self action:@selector(bindClick) forControlEvents:UIControlEventTouchUpInside];
  [contenView addSubview:submitButton];

  //! tip
  UILabel *registTipLabel = [[UILabel alloc] initWithFrame:CGRectMake(registButton.yt_left, registButton.yt_bottom + 10, registButton.yt_width, 12)];
  registTipLabel.text = [NSString stringWithFormat:@"%@?",YTLocalizedString(@"还未有油咭")];
  registTipLabel.textAlignment = NSTextAlignmentCenter;
  registTipLabel.font = [UIFont systemFontOfSize:11.0f - fontOffset];
  registTipLabel.textColor = DEF_GRAY;
  [contenView addSubview:registTipLabel];


  
}

- (void)sendCode {
  ///! 
  if (self.codeButton.selected == YES) {
    return;
  }
  [self countDownT];
  
}

///! 申请
- (void)cardRegistClick {
//  if (self.applyClick) {
//    self.applyClick();
//  }
  
  ApplyOilFirstVC *firstVC = [[ApplyOilFirstVC alloc] init];
  
  BaseNavigationVC *firstNaV = [[BaseNavigationVC alloc] initWithRootViewController:firstVC];
  [self presentViewController:firstNaV animated:YES completion:nil];
  
  
}

///! 绑定
- (void)bindClick {
  if (self.loginSuccessed) {
    self.loginSuccessed();
  }
}

- (void)cancelClick {
  [self.view endEditing:YES];
  [self goBackVC];
}

- (void)countDownT {
  dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
  dispatch_block_t block = ^{
    int i = 60;
    while (i > 0) {
      if (i != 60) sleep(1);
      //耗时的操作
      NSLog(@"倒计时60秒----%d",i);
      i--;
      dispatch_async(dispatch_get_main_queue(), ^{
        if (self.codeButton) {
          if (i > 0) {
            // HEXCOLOR(0X888888)
            [self.codeButton setTitleColor:DEF_GRAY forState:UIControlStateNormal];
            [self.codeButton setTitle:[NSString stringWithFormat:@"%dS", i] forState:UIControlStateNormal];
            
            if (self.codeButton.selected == NO) {
              self.codeButton.selected = YES;
            }
            
          } else {
            [self.codeButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            self.codeButton.selected = NO;
            [self.codeButton setTitle:YTLocalizedString(@"获取验证码") forState:UIControlStateNormal];
          }
        }
      });
    }
  };
  dispatch_async(queue, block);
}


@end

